﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace be.metanous.mvvm.utils
{
    public class TypeDataTemplateSelector : DataTemplateSelector
    {
        private Dictionary<Type, DataTemplate> _dataTemplates = new Dictionary<Type, DataTemplate>();

        public Dictionary<Type, DataTemplate> DataTemplates { get { return _dataTemplates; }  }

        public TypeDataTemplateSelector()
        {
            
        }

        public TypeDataTemplateSelector(Type type, DataTemplate dataTemplate)
        {
            _dataTemplates.Add(type, dataTemplate);
        }

        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
          
            if (DataTemplates.ContainsKey(item.GetType()))
            {
                var dataTemplate = DataTemplates[item.GetType()];
                if (dataTemplate != null)
                {
                    return dataTemplate;
                }
            }
            return base.SelectTemplate(item, container);
        }
    }
}
